package com.gzhryc.shared_device.oem.code.agent.services;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.oem.code.agent.dao.AgentMenuDao;
import com.gzhryc.shared_device.oem.code.agent.dao.db.AgentMenu;
import com.gzhryc.shared_device.oem.code.agent.services.dto.AgentMenuSearch;

public class AgentMenuService {

    static Logger log = Logger.getLogger(AgentMenuService.class);

    public static AgentMenuService self() {
        return MultiDBTools.getService(AgentMenuService.class, true);
    }

    public static AgentMenuService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, AgentMenuService.class, true);
    }

    AgentMenuDao dao;

    public AgentMenuService(String jdbcKey) {
        this.dao = new AgentMenuDao(jdbcKey);
    }

    public List<AgentMenu> findBySearch(AgentMenuSearch search, Integer pageNo, Integer pageLimit){
        Conditions conditions = dao.buildConditions(search);
        conditions.asc("parentId");
        conditions.asc("priority");
        conditions.asc("id");

        try {
            return dao.findByCondition(conditions,pageNo,pageLimit);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public Long countBySearch(AgentMenuSearch search){
        Conditions conditions = dao.buildConditions(search);

        try {
            return dao.count(conditions);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return 0L;
    }

    public List<AgentMenu> findByParentId(Long parentId){
        if(parentId != null){
            Conditions conditions = new Conditions(AgentMenu.class);
            conditions.and().is("parentId",parentId);

            try {
                return dao.findByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public List<AgentMenu> findRoot(){
        Conditions conditions = new Conditions(AgentMenu.class);
        conditions.and().isNull("parentId");

        try {
            return dao.findByCondition(conditions);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public List<AgentMenu> findByParentAndType(Long parentId,Integer type){
        if(parentId != null && type != null){
            Conditions conditions = new Conditions(AgentMenu.class);
            conditions.and().is("parentId",parentId);
            conditions.and().is("type",type);

            try {
                return dao.findByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public AgentMenu getById(Long id){
        try {
            return dao.getById(id);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public boolean add(AgentMenu agentMenu) throws LogicException {
        agentMenu.setCreateDate(new Date());
        try {
            return dao.insert(agentMenu) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean updateNotNull(AgentMenu agentMenu) throws LogicException {
        try {
            return dao.updateNotNull(agentMenu) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean deleteById(Long id) throws LogicException {
        try {
            return dao.deleteById(id) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }
}
